package io.codegeneratetool.web.service;

import io.codegeneratetool.util.StringUtils;
import io.codegeneratetool.web.common.PageRequest;
import io.codegeneratetool.web.common.PagerResult;
import io.codegeneratetool.web.entity.Setting;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

@Service("settingService")
public class SettingService {

	
	
	@Resource(name="jdbcTemplate_codegen")
	JdbcTemplate jdbcTemplate;
	
	public int createOrUpdateEntity(Setting setting){
		String  sql;
		if(StringUtils.isNullOrEmpty(setting.getDbid())){
			sql="insert into CODE_SETTING(dbid,createdtime,dbconn_dbid,name) values(?,?,?,?)";
			return jdbcTemplate.update(sql,StringUtils.getUUID()
					, new Date() 
					,setting.getDbconn_dbid()
					,setting.getName() 
					);
		}else{
			sql="update CODE_SETTING set name=?  where dbid=?";
			return jdbcTemplate.update(sql ,
					setting.getName() 
					,setting.getDbid()
					);
		}
		 
	}
	
	
	public PagerResult findAll(PageRequest pageRequest){ 
		String countSql = "select count(*) from CODE_SETTING ";
		String sql = "select * from CODE_SETTING  order by createdtime limit ?,?";
		Long queryForObject = jdbcTemplate.queryForObject(countSql, Long.class);
		List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql,pageRequest.getFirst(),pageRequest.getPagesize());
		PagerResult result = new PagerResult (pageRequest.getPage(), queryForObject.intValue(), queryForList.size() , queryForList);
		return result;
		
	}
	public List<Setting> findByFK(String id){ 
		String sql = "select * from CODE_SETTING where dbconn_dbid=?";
		
		try {
			List<Setting> ss = new ArrayList<Setting>();
			SqlRowSet rs = jdbcTemplate.queryForRowSet(sql, id); 
		 	while(rs.next()){
		 		Setting connection = new Setting();
				connection.setDbid(rs.getString("DBID"));
				connection.setCreatedtime(new Timestamp(rs.getLong("CREATEDTIME")) ); 
				connection.setName(rs.getString("NAME")); 
				connection.setDbconn_dbid(rs.getString("DBCONN_DBID"));
				ss.add(connection);
		 	}
			 return ss;
		} catch ( Exception e) {
			return null;
		} 
	}
	
	
	public Setting findById(String id){ 
		String sql = "select * from CODE_SETTING where dbid=?";
		
		try {
			return jdbcTemplate.queryForObject(sql,new Object[]{id},new RowMapper<Setting>(){

				public Setting mapRow(ResultSet rs, int idx)
						throws SQLException {
					Setting connection = new Setting();
					connection.setDbid(rs.getString("DBID"));
					connection.setCreatedtime(new Timestamp( rs.getLong("CREATEDTIME") )); 
					connection.setName(rs.getString("NAME")); 
					connection.setDbconn_dbid(rs.getString("DBCONN_DBID")); 
					return connection;
				}
				
			});
		} catch ( Exception e) {
			return null;
		} 
	}
	
	
	public int del(String id){ 
		String sql = "delete from CODE_SETTING where dbid=?";
		return jdbcTemplate.update(sql,  id ); 
	}
	
}
